package controllers;

import models.MenuItem;
import play.cache.Cache;
import play.mvc.Before;
import play.mvc.Controller;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Cyril
 * Date: 13.08.11
 * Time: 22:15
 * To change this template use File | Settings | File Templates.
 */
public class MenuBuilder extends Controller {
    @Before
    @SuppressWarnings("unchecked")
    public static void buildMenu() throws Throwable {
        String chName = getMenuCacheName();
        List<MenuItem> mainMenu = Cache.get(chName, List.class);
        if (mainMenu == null) {
            mainMenu = MenuItem.all()
                .order("order")
                .fetch();
            Cache.set(chName, mainMenu);
        }
        renderArgs.put( "mainMenu", mainMenu );
    }

    public static String getMenuCacheName() throws Throwable {
        return session.getId() + "mainMenu" +
                (CustomSecure.check(new String[]{"admin"}) ? "-admin" : "");
    }
}
